import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import TensorDataset, DataLoader
import matplotlib.pyplot as plt
from sklearn.metrics import precision_score, recall_score
from gen_corr import get_corr_feature

# Set random seed for reproducibility
np.random.seed(42)
torch.manual_seed(42)
cols = get_corr_feature()

# Load data
train_df = pd.read_csv('data_c1.csv')
test_df_4 = pd.read_csv('data_c4.csv')
test_df_6 = pd.read_csv('data_c6.csv')

# Separate features and labels
X_train = train_df.drop('label', axis=1)[cols].values
y_train = train_df['label'].values
X_test_4 = test_df_4.drop('label', axis=1)[cols].values
y_test_4 = test_df_4['label'].values
X_test_6 = test_df_6.drop('label', axis=1)[cols].values
y_test_6 = test_df_6['label'].values

# Normalize data
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test_4 = scaler.transform(X_test_4)
X_test_6 = scaler.transform(X_test_6)

# Convert labels to one-hot encoding
encoder = OneHotEncoder(sparse=False)
y_train = encoder.fit_transform(y_train.reshape(-1, 1))
y_test_4 = encoder.transform(y_test_4.reshape(-1, 1))
y_test_6 = encoder.transform(y_test_6.reshape(-1, 1))

# Convert to PyTorch tensors
X_train = torch.tensor(X_train, dtype=torch.float).unsqueeze(1)
y_train = torch.tensor(y_train, dtype=torch.float)
X_test_4 = torch.tensor(X_test_4, dtype=torch.float).unsqueeze(1)
y_test_4 = torch.tensor(y_test_4, dtype=torch.float)
X_test_6 = torch.tensor(X_test_6, dtype=torch.float).unsqueeze(1)
y_test_6 = torch.tensor(y_test_6, dtype=torch.float)

# Create data loaders
batch_size = 32
train_loader = DataLoader(TensorDataset(X_train, y_train), batch_size=batch_size, shuffle=True)
test_loader_4 = DataLoader(TensorDataset(X_test_4, y_test_4), batch_size=batch_size)
test_loader_6 = DataLoader(TensorDataset(X_test_6, y_test_6), batch_size=batch_size)


# Define the GRU model
class GRU(nn.Module):
    def __init__(self, input_dim, hidden_dim, output_dim, num_layers):
        super(GRU, self).__init__()
        self.hidden_dim = hidden_dim
        self.num_layers = num_layers
        self.gru = nn.GRU(input_dim, hidden_dim, num_layers, batch_first=True)
        self.fc = nn.Linear(hidden_dim, output_dim)

    def forward(self, x):
        h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).to(x.device)
        out, _ = self.gru(x, h0)
        out = self.fc(out[:, -1, :])
        return out


# Instantiate the model, loss function, and optimizer
input_dim = X_train.shape[2]  # Number of features
hidden_dim = 128  # Dimension of the hidden layer in GRU
output_dim = 3  # Number of output classes
num_layers = 2  # Number of GRU layers

model = GRU(input_dim=input_dim, hidden_dim=hidden_dim, output_dim=output_dim, num_layers=num_layers)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
test_accuracies_4 = []
test_accuracies_6 = []

# Train the model
num_epochs = 30
for epoch in range(num_epochs):
    model.train()
    for inputs, labels in train_loader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, torch.max(labels, 1)[1])
        loss.backward()
        optimizer.step()
    print(f'Epoch {epoch + 1}, Loss: {loss.item()}')

    # Evaluate the model on test set
    model.eval()
    # Evaluations for test set 4 and test set 6 are similar to above and can be repeated here.vx:15234405680
    total = 0
    correct = 0
    with torch.no_grad():
        for inputs, labels in test_loader_4:
            outputs = model(inputs)
            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted == torch.max(labels, 1)[1]).sum().item()
    test_accuracy_4 = correct / total
    test_accuracies_4.append(test_accuracy_4)
    print(f'Epoch {epoch + 1}, Test Accuracy_4: {test_accuracy_4:.4f}')

    total = 0
    correct = 0

    with torch.no_grad():
        for inputs, labels in test_loader_6:
            outputs = model(inputs)
            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted == torch.max(labels, 1)[1]).sum().item()
    test_accuracy_6 = correct / total
    test_accuracies_6.append(test_accuracy_6)
    print(f'Epoch {epoch + 1}, Test Accuracy_6: {test_accuracy_6:.4f}')

plt.figure(figsize=(12, 5))
plt.plot(test_accuracies_4, label='Test_4 Accuracy', marker='o')
plt.plot(test_accuracies_6, label='Test_6 Accuracy', marker='x')
plt.title('Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()

plt.savefig('gru_acc.png')
plt.close()
